/*
 * ComponenteAS.java
 *
 * Created on August 12, 2007, 4:44 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package tcc.applicationService.Componente;

import java.util.List;
import javax.persistence.EntityTransaction;
import tcc.applicationService.AbstractAS;
import tcc.dao.produto.ComponenteDao;
import tcc.exception.system.TccASException;
import tcc.exception.system.TccSystemException;
import tcc.util.PersistenceUtil;
import tcc.domain.produto.Componente;


public class ComponenteAS extends AbstractAS {

    /** Creates a new instance of ComponenteAS */
    private ComponenteAS() {
    }
    private static ComponenteAS instance;

    public static synchronized ComponenteAS getInstance() {
        if (instance == null) {
            instance = new ComponenteAS();
        }
        return instance;
    }

    public void salvarComponente(Componente c) throws TccSystemException {

        EntityTransaction tx = PersistenceUtil.getInstance().getTransaction();

        try {
            tx.begin();
            new ComponenteDao().save(c);
            tx.commit();
        } catch (Exception ex) {
            tx.rollback();
            throw new TccSystemException(ex);
        }
    }

    public void removerComponente(Componente c) throws TccASException {
        
        EntityTransaction tx = PersistenceUtil.getInstance().getTransaction();
        
        try {
            tx.begin();
            new ComponenteDao().delete(c);
            tx.commit();
        } catch (Exception ex) {
            tx.rollback();
            throw new TccASException(ex);
        }
    }

    public List<Componente> listarComponentes() throws TccASException {
        ComponenteDao dao = new ComponenteDao();
        return dao.findAll();
    }

    public Componente buscarPorId(Long Id) throws TccASException {
        try {
            return new ComponenteDao().findById(Id);
        } catch (Exception ex) {
            throw new TccASException(ex);
        }
    }
}